package com.by.cloud.controller;

import cn.hutool.core.collection.CollUtil;
import com.by.cloud.annotation.PreAuthorize;
import com.by.cloud.common.BaseResponse;
import com.by.cloud.common.PageResult;
import com.by.cloud.common.ResultUtils;
import com.by.cloud.enums.UserRoleEnum;
import com.by.cloud.exception.ErrorCode;
import com.by.cloud.exception.ThrowUtils;
import com.by.cloud.model.dto.user.*;
import com.by.cloud.model.vo.user.UserLoginVo;
import com.by.cloud.model.vo.user.UserVo;
import com.by.cloud.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author lzh
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户模块")
@Slf4j
public class UserController {

    @Resource
    private UserService userService;

    @ApiOperation(value = "用户注册")
    @PostMapping("/register")
    public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
        ThrowUtils.throwIf(userRegisterRequest == null, ErrorCode.PARAMS_ERROR);
        long userId = userService.userRegister(userRegisterRequest);
        return ResultUtils.success(userId);
    }

    @ApiOperation("根据ID删除单个用户")
    @PreAuthorize(role = UserRoleEnum.ADMIN)
    @DeleteMapping("/delete/{userId}}")
    public BaseResponse<Boolean> deleteUserById(@PathVariable Long userId) {
        ThrowUtils.throwIf(userId == null || userId <= 0, ErrorCode.PARAMS_ERROR);
        boolean isSuccess = userService.removeUserById(userId);
        return ResultUtils.success(isSuccess);
    }

    @ApiOperation("批量删除用户")
    @PreAuthorize(role = UserRoleEnum.ADMIN)
    @DeleteMapping("/delete")
    public BaseResponse<Boolean> deleteUser(@RequestParam List<Long> ids) {
        ThrowUtils.throwIf(CollUtil.isEmpty(ids), ErrorCode.PARAMS_ERROR);
        boolean success = userService.removeBatchByIds(ids);
        return ResultUtils.success(success);
    }

    @ApiOperation("根据ID查询用户")
    @GetMapping("/getOne/{userId}")
    @PreAuthorize(role = UserRoleEnum.ADMIN)
    public BaseResponse<UserVo> getUserById(@PathVariable Long userId) {
        ThrowUtils.throwIf(userId == null || userId <= 0, ErrorCode.PARAMS_ERROR);
        UserVo userVo = userService.getUserById(userId);
        return ResultUtils.success(userVo);
    }

    @ApiOperation("根据ID集合批量查询用户")
    @PreAuthorize(role = UserRoleEnum.ADMIN)
    @GetMapping("/getUserList")
    public BaseResponse<List<UserVo>> getUserByIdList(@RequestParam List<Long> userIds) {
        ThrowUtils.throwIf(CollUtil.isEmpty(userIds), ErrorCode.PARAMS_ERROR);
        List<UserVo> userVoList = userService.getUserByIdList(userIds);
        return ResultUtils.success(userVoList);
    }

    @ApiOperation("分页查询用户")
    @PreAuthorize(role = UserRoleEnum.ADMIN)
    @PostMapping("/page")
    public BaseResponse<PageResult<UserVo>> pageUsers(@RequestBody UserPageRequest userPageRequest) {
        ThrowUtils.throwIf(userPageRequest == null, ErrorCode.PARAMS_ERROR);
        PageResult<UserVo> result = userService.pageUsers(userPageRequest);
        return ResultUtils.success(result);
    }

    @ApiOperation(value = "用户登录")
    @PostMapping("/login")
    public BaseResponse<UserLoginVo> login(@RequestBody UserLoginDto userLoginDto) {
        log.info("用户登录，{}", userLoginDto);
        UserLoginVo loginVo = userService.login(userLoginDto);
        return ResultUtils.success(loginVo);
    }

    @ApiOperation("获取当前登录用户信息")
    @GetMapping("/currentUserInfo")
    public BaseResponse<UserVo> getCurrentUserInfo() {
        UserVo userVo = userService.getCurrentUserInfo();
        return ResultUtils.success(userVo);
    }

    @ApiOperation("更新用户信息")
    @PostMapping("/update")
    public BaseResponse<Boolean> update(@RequestBody UserUpdateDto userDto) {
        boolean isSuccess = userService.updateByUserInfo(userDto);
        return ResultUtils.success(isSuccess);
    }

    @ApiOperation("管理员更新用户信息")
    @PostMapping("/updateAdmin")
    @PreAuthorize(role = UserRoleEnum.ADMIN)
    public BaseResponse<Boolean> updateByAdmin(@RequestBody UserUpdateAdminDto userDto) {
        boolean isSuccess = userService.updateByAdmin(userDto);
        return ResultUtils.success(isSuccess);
    }

    @ApiOperation("是否冻结用户")
    @GetMapping("/freeze")
    @PreAuthorize(role = UserRoleEnum.ADMIN)
    public BaseResponse<Boolean> freezeByUserId(@RequestParam List<Long> userIds, Integer status) {
        boolean isSuccess = userService.freezeByUserId(userIds, status);
        return ResultUtils.success(isSuccess);
    }

    @ApiOperation("上传头像")
    @PostMapping("/avatar/upload")
    public BaseResponse<String> uploadAvatar(@RequestPart("file") MultipartFile multipartFile) {
        ThrowUtils.throwIf(multipartFile == null, ErrorCode.PARAMS_ERROR);
        String avatarUrl = userService.uploadAvatar(multipartFile);
        return ResultUtils.success(avatarUrl);
    }
    @ApiOperation("判断是否为管理员")
    @GetMapping("/isAdmin/{userId}")
    public BaseResponse<Boolean> isAdmin(@PathVariable("userId") Long userId){
        boolean result = userService.isAdmin(userId);
        return ResultUtils.success(result);
    }
}
